package com.winona.ui;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.taplinker.client.R;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.core.util.Base64;
import com.taplinker.core.util.DeviceUtil;
import com.winona.ConstantValue;
import com.winona.code.LoginCmd;
import com.winona.widget.LoadingDialog;

public class LoginActivity extends Activity {
	private TextView title;
	private Button bt_login;

	private EditText et_username, et_password, et_verify;
	private ImageView image_verify;
	private LoadingDialog mDialog;

	private String codetoken = "";
	private boolean relogin = false;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// SharedPreferences sp = getSharedPreferences("winona",
		// MODE_WORLD_WRITEABLE | MODE_WORLD_READABLE);
		setContentView(R.layout.login_activity);
		findView();
		init();
		getLocalUserInfo();
		loadVerifyImage();
	}

	private void getLocalUserInfo() {
		try {
			SharedPreferences sp = getSharedPreferences(Welcome.SP_FILE_NAME,
					Context.MODE_PRIVATE);
			et_username.setText(sp.getString(Welcome.SP_KEY_USERNAME, ""));
			et_password.setText(sp.getString(Welcome.SP_KEY_PASSWORD, ""));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onResume() {

		super.onResume();
	}

	private void loadVerifyImage() {
		new SimpleBaseCommand() {

			@Override
			public Result go() {
				return new HttpTemplate().exchange(ConstantValue.COMMON
						+ ConstantValue.VERIFY);
			}

			@Override
			public void yes(Result result) {
				try {
					JSONObject jo = new JSONObject(result.object.toString())
							.getJSONObject("body");
					byte[] bts = Base64.decode(jo.getString("image"));
					Bitmap bmp = BitmapFactory.decodeByteArray(bts, 0,
							bts.length);
					image_verify.setImageBitmap(bmp);
					codetoken = jo.getString("key");
				} catch (Exception e) {
					e.printStackTrace();
					no(null);
				}
			}

			@Override
			public void no(Result result) {
				Toast.makeText(LoginActivity.this, "获取验证码失败， 请刷新验证码",
						Toast.LENGTH_SHORT).show();
			}

		}.execute();
	}

	private void init() {
		bt_login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkInput()) {
					mDialog.show();
					doLogin();
				}
			}
		});
		image_verify.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadVerifyImage();
			}
		});
	}

	private void doLogin() {
		new LoginCmd(this, mDialog, et_username.getText().toString(),
				et_password.getText().toString(), et_verify.getText()
						.toString(), codetoken, relogin).execute();
	}

	private void findView() {
		title = (TextView) findViewById(R.id.login_title);
		title.setText("专家登录");
		bt_login = (Button) findViewById(R.id.bt_login);

		et_username = (EditText) findViewById(R.id.et_username);
		et_password = (EditText) findViewById(R.id.et_password);
		et_verify = (EditText) findViewById(R.id.et_Verify);
		image_verify = (ImageView) findViewById(R.id.image_verify);

		et_verify.setText("");

		mDialog = new LoadingDialog(this);
		mDialog.setText("正在登录...");
		mDialog.setCancelable(true);
		mDialog.setCanceledOnTouchOutside(true);
	}

	private boolean checkInput() {
		String username = et_username.getText().toString();
		String password = et_password.getText().toString();

		String code = et_verify.getText().toString();
		if (!DeviceUtil.isNetworkAvailable(getApplicationContext())) {
			showToast("当前网络不可用");
			return false;
		}
		if (TextUtils.isEmpty(username)) {
			Toast.makeText(this, "请输入邮箱地址", Toast.LENGTH_SHORT).show();

			return false;
		}
		if (TextUtils.isEmpty(password)) {
			Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();

			return false;
		}
		if (TextUtils.isEmpty(code)) {
			Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	public void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

}
